package com.taylor.seckill.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author taylor
 * @Description: exchange有4个类型：direct，topic，fanout，header(几乎不用)。
 * 一般来说direct和topic用来具体的路由消息，如果要用广播的消息一般用fanout的exchange。
 * header类型用的比较少
 * @date 2021/4/15 23:25
 */
@Configuration
public class RabbitmqConfig {

    //fanout 模式交换机
    public static final String FANOUT_QUEUE_1 = "fanoutQueue1";
    public static final String FANOUT_QUEUE_2 = "fanoutQueue2";
    public static final String FANOUT_EXCHANGE = "fanoutExchange";

    //direct 模式交换机
    public static final String DIRECT_QUEUE_1 = "directQueue1";
    public static final String DIRECT_QUEUE_2 = "directQueue2";
    public static final String DIRECT_EXCHANGE ="directExchange";
    public static final String DIRECT_ROUTE_1 = "directRoute1";
    public static final String DIRECT_ROUTE_2 = "directRoute2";

    //topic 模式交换机
    public static final String TOPIC_QUEUE_1 = "topicQueue1";
    public static final String TOPIC_QUEUE_2 = "topicQueue2";
    public static final String TOPIC_EXCHANGE ="topicExchange";

    public static final String TOPIC_ROUTE_1 = "#.topic.#";//'#'则表示匹配没有或者多个单词
    public static final String TOPIC_ROUTE_2 = "*.topic.*";//'*'表示匹配一个单词

//-------------------------------------不配置 则采用默认 AMQP default交换机---------------------------------

    @Bean
    public Queue queue(){
        return new Queue("queue",true);
    }
//-------------------------------------fanout 模式交换机begin-----------------------------------------------
    /**
     * fanout 模式交换机
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange(FANOUT_EXCHANGE);
    }
    @Bean
    public Queue fanoutQueue1(){
        return new Queue(FANOUT_QUEUE_1);
    }
    @Bean
    public Queue fanoutQueue2(){
        return new Queue(FANOUT_QUEUE_2);
    }
    /**
     *  队列绑定到交换机
     * @return
     */
    @Bean
    public Binding binding1(){
        return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
    }
    @Bean
    public Binding binding2(){
        return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
    }

//----------------------------------------fanout 模式交换机end--------------------------------------------

//----------------------------------------direct 模式交换机begin------------------------------------------

    @Bean
    public Queue directQueue1(){
        return new Queue(DIRECT_QUEUE_1);
    }
    @Bean
    public Queue directQueue2(){
        return new Queue(DIRECT_QUEUE_2);
    }
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    @Bean
    public Binding bindingDirectQueue1(){
        return BindingBuilder.bind(directQueue1()).to(directExchange()).with(DIRECT_ROUTE_1);
    }
    @Bean
    public Binding bindingDirectQueue2(){
        return BindingBuilder.bind(directQueue2()).to(directExchange()).with(DIRECT_ROUTE_2);
    }

//----------------------------------------direct 模式交换机end--------------------------------------------


//----------------------------------------topic 模式交换机begin-----------------------------------------


    @Bean
    public Queue topicQueue1(){
        return new Queue(TOPIC_QUEUE_1);
    }
    @Bean
    public Queue topicQueue2(){
        return new Queue(TOPIC_QUEUE_2);
    }
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(TOPIC_EXCHANGE);
    }

    @Bean
    public Binding bindingTopicQueue1(){
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with(TOPIC_ROUTE_1);
    }
    @Bean
    public Binding bindingTopicQueue2(){
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with(TOPIC_ROUTE_2);
    }
//----------------------------------------topic 模式交换机end--------------------------------------------
}
